//
//  WXPProductPackageCell.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/13.
//
//

#import "WXPProductPackageCell.h"

@interface WXPProductPackageCell ()

@end

@implementation WXPProductPackageCell

#pragma mark - lazy
- (UIView *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIView new];
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, 0);
        _viewBack.userInteractionEnabled = true;
    }
    return _viewBack;
}
- (UIImageView *)imageLogo{
    if (_imageLogo == nil) {
        _imageLogo = [UIImageView new];
        _imageLogo.contentMode = UIViewContentModeScaleAspectFill;
        _imageLogo.clipsToBounds = YES;
        _imageLogo.widthHeight = XY(36, 36);
        [GlobalMethod setRoundView:_imageLogo color:[UIColor clearColor] numRound:8 width:0];
    }
    return _imageLogo;
}
- (UIButton *)btnArrow{
    if (_btnArrow == nil) {
        _btnArrow = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnArrow.tag = 1;
        [_btnArrow setImage:[UIImage imageNamed:@"icon-arrow-detail"] forState:UIControlStateNormal];
        [_btnArrow setImage:[UIImage imageNamed:@"icon-arrow-down"] forState:UIControlStateSelected];
        _btnArrow.widthHeight = XY(14+LEFT_MARDIN*2, 14+LEFT_MARDIN*2);
        [_btnArrow addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnArrow;
}
- (UIButton *)btnOpen{
    if (_btnOpen == nil) {
        _btnOpen = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnOpen.tag = 1;
        [_btnOpen addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnOpen;
}
- (UILabel *)labelName{
    if (_labelName == nil) {
        _labelName = [UILabel new];
        _labelName.textColor = COLOR_BLACK;
        _labelName.font = [UIFont boldSystemFontOfSize:14];
    }
    return _labelName;
}
- (UILabel *)labelMoney{
    if (_labelMoney == nil) {
        _labelMoney = [UILabel new];
        _labelMoney.textColor = COLOR_BLACK;
        _labelMoney.font = [UIFont boldSystemFontOfSize:20];
    }
    return _labelMoney;
}
- (UIView *)viewContent{
    if (_viewContent == nil) {
        _viewContent = [UIView new];
        _viewContent.backgroundColor = [UIColor colorWithHexString:@"F6F6F6"];
        _viewContent.widthHeight = XY(self.viewBack.width-2*LEFT_MARDIN, 0);
        [GlobalMethod setRoundView:_viewContent color:[UIColor clearColor] numRound:16 width:0];
    }
    return _viewContent;
}
- (UILabel *)labelPeriodTitle{
    if (_labelPeriodTitle == nil) {
        _labelPeriodTitle = [UILabel new];
        _labelPeriodTitle.textColor = COLOR_CONTENT;
        _labelPeriodTitle.fontNum = (12);
    }
    return _labelPeriodTitle;
}
- (UILabel *)labelPeriod{
    if (_labelPeriod == nil) {
        _labelPeriod = [UILabel new];
        _labelPeriod.textColor = COLOR_BLACK;
        _labelPeriod.fontNum = 12;
    }
    return _labelPeriod;
}
- (UILabel *)labelInterestTitle{
    if (_labelInterestTitle == nil) {
        _labelInterestTitle = [UILabel new];
        _labelInterestTitle.textColor = COLOR_CONTENT;
        _labelInterestTitle.fontNum = 12;
    }
    return _labelInterestTitle;
}
- (UILabel *)labelInterest{
    if (_labelInterest == nil) {
        _labelInterest = [UILabel new];
        _labelInterest.textColor = COLOR_BLACK;
        _labelInterest.fontNum = 12;
    }
    return _labelInterest;
}
- (UILabel *)labelTotalMoneyTitle{
    if (_labelTotalMoneyTitle == nil) {
        _labelTotalMoneyTitle = [UILabel new];
        _labelTotalMoneyTitle.textColor = COLOR_CONTENT;
        _labelTotalMoneyTitle.fontNum = 12;
    }
    return _labelTotalMoneyTitle;
}
- (UILabel *)labelTotalMoney{
    if (_labelTotalMoney == nil) {
        _labelTotalMoney = [UILabel new];
        _labelTotalMoney.textColor = COLOR_BLACK;
        _labelTotalMoney.fontNum = 12;
    }
    return _labelTotalMoney;
}
- (UILabel *)labelTimeTitle{
    if (_labelTimeTitle == nil) {
        _labelTimeTitle = [UILabel new];
        _labelTimeTitle.textColor = COLOR_CONTENT;
        _labelTimeTitle.fontNum = 12;
    }
    return _labelTimeTitle;
}
- (UILabel *)labelTime{
    if (_labelTime == nil) {
        _labelTime = [UILabel new];
        _labelTime.textColor = COLOR_BLACK;
        _labelTime.fontNum = 12;
    }
    return _labelTime;
}


#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpView];
    }
    return self;
}

- (void)setUpView{
    [self.contentView addSubview:self.viewBack];
    [self.viewBack addSubview:self.imageLogo];
    [self.viewBack addSubview:self.btnArrow];
    [self.viewBack addSubview:self.btnOpen];
    [self.viewBack addSubview:self.labelName];
    [self.viewBack addSubview:self.labelMoney];
    [self.viewBack addSubview:self.viewContent];
    [self.viewContent addSubview:self.labelPeriodTitle];
    [self.viewContent addSubview:self.labelPeriod];
    [self.viewContent addSubview:self.labelInterestTitle];
    [self.viewContent addSubview:self.labelInterest];
    [self.viewContent addSubview:self.labelTotalMoneyTitle];
    [self.viewContent addSubview:self.labelTotalMoney];
    [self.viewContent addSubview:self.labelTimeTitle];
    [self.viewContent addSubview:self.labelTime];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    switch (sender.tag) {
        case 1:
        {
            if(self.blockOpenOrCloseDetail){
                self.blockOpenOrCloseDetail(self.model);
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 刷新cell
- (void)resetCellWithModel:(ModelProductPackageList *)model{
    self.model = model;
    self.imageLogo.leftTop =  XY(LEFT_MARDIN, 13);
    [self.imageLogo sd_setImageWithUrlStr:readStr(model.slogan) placeholderImage:@""];
    
    self.btnOpen.widthHeight = XY(self.viewBack.width, self.imageLogo.bottom+10);
    self.btnOpen.leftTop = XY(0, 0);
    self.btnArrow.rightCenterY = XY(self.viewBack.width, self.imageLogo.centerY);
    [self.labelName fitTitle:readStr(model.name) variable:0];
    self.labelName.leftCenterY = XY(self.imageLogo.right+12, self.imageLogo.centerY);
    [self.labelMoney fitTitle:[NSString stringWithFormat:@"$%@",[GlobalMethod showMoney:strDot2F(model.amount)]] variable:0];
    self.labelMoney.rightCenterY = XY(self.btnArrow.left+1, self.imageLogo.centerY);
    self.viewContent.hidden = !model.isShowDetail;
    self.btnArrow.selected = !self.viewContent.hidden;
    
    CGFloat labelTop = LEFT_MARDIN;
    if(isAry(model.pairs)){
        [self.viewContent removeAllSubViews];
        for (int i = 0; i < model.pairs.count; i ++) {
            ModelPairs * modelPairs = model.pairs[i];
            UILabel * labelTitle = [UILabel new];
            [GlobalMethod setLabel:labelTitle widthLimit:0 fontNum:12 textColor:COLOR_CONTENT text:readStr(modelPairs.key)];
            labelTitle.leftTop = XY(LEFT_MARDIN, labelTop);
            [self.viewContent addSubview:labelTitle];
            labelTop = labelTitle.bottom+LEFT_MARDIN;
            UILabel * labelContent = [UILabel new];
            [GlobalMethod setLabel:labelContent widthLimit:0 fontNum:12 textColor:COLOR_BLACK text:readStr(modelPairs.value)];
            labelContent.rightCenterY = XY(self.viewContent.width-LEFT_MARDIN, labelTitle.centerY);
            [self.viewContent addSubview:labelContent];
        }
    }
    self.viewContent.height = labelTop;
    self.viewContent.centerXTop = XY(self.viewBack.width/2, self.imageLogo.bottom+10);
    
    self.viewBack.height = self.viewContent.hidden == true ? (self.imageLogo.bottom + self.imageLogo.top) : (self.viewContent.bottom+LEFT_MARDIN);
    self.viewBack.centerXTop = XY(SCREEN_WIDTH/2, 0);
    self.height = self.viewBack.bottom;
}

@end
